home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / amiexpress / source / utils / mailstat / msgbaserepair.c < prev   
Encoding:
C/C++ Source or Header  |  1992-12-26  |  7.7 KB  |  325 lines

  1. /******
  2.  for Manx compile with -WO option
  3. *******/
  4.  
  5. #include <exec/types.h>
  6. #include <exec/exec.h>
  7. #include <dos/dos.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include "dh0:code/code20/defines.h"
  11. struct MailHeader MS;
  12. char conf[200];
  13. struct MailStat OMS;
  14. BOOL UPDATE=FALSE;
  15. void NullStats();
  16. int TLock(char *str);
  17. void sr(char *str);
  18. char temp[200];
  19. long Slot;
  20. long GetFileSize(char *str);
  21. long FileSize=0L;
  22. long TotalSlots=0L;
  23. long TempSlot=0L;
  24. char TempFile[200];
  25. char StatFile[200];
  26. void InitStat();
  27. BOOL StatError=TRUE;
  28. int ValidStat();
  29. long GetStat();
  30. long StatNum;
  31. BOOL Eoj=TRUE;
  32. void end();
  33. main(int argc,char *argv[])
  34. {
  35.   FILE *fi,*fo;
  36.   if(argc<2 || argc >3)
  37.   {
  38.     printf("\n\n");
  39.     printf("MsgBaseRepair version 1.0 written by Joseph Hodge\n");
  40.     printf("Usage: MsgBaseRepair <conference path> [UPDATE]\n");
  41.     printf("   ie: MsgBaseRepair BBS:PD\n");
  42.     printf("\n");
  43.     printf("WARNING! This utility will not make a backup and\n");
  44.     printf("         will attempt to fix your msgbase if it\n");
  45.     printf("         detects any errors\n\n");
  46.     printf("         THE BBS MUST BE OFFLINE WHEN RUNNING THIS\n");
  47.     printf("         UTILITY\n");
  48.     printf("This is an AmiExpress 1.1w+ utility\n");
  49.  
  50.     printf("\n");
  51.     exit(0);
  52.   }
  53.   printf("\n\nMsgBaseRepair version 1.0\nwritten by Joseph Hodge\n");
  54.     
  55.   strcpy(conf,argv[1]);
  56.   sr(conf);
  57.   if(!TLock(conf))
  58.   {
  59.      printf("Can't locate conference location %s\n",conf);
  60.      printf("\n");
  61.      exit(0);
  62.   }
  63.   Slot=0L;
  64.   if(argc==3)
  65.   {
  66.     UPDATE=TRUE;
  67.   }
  68.   strcpy(StatFile,conf);
  69.   strcat(StatFile,"/MsgBase/MailStats");
  70.   if(!TLock(StatFile))
  71.   {
  72.     StatError=TRUE; StatNum=-1L;
  73.     Eoj=FALSE;
  74.   }else
  75.   { StatError=FALSE; StatNum=GetStat(); }
  76.   strcat(conf,"/MsgBase/HeaderFile");
  77.   NullStats();
  78. printf("*---------------------------------------------*\n");
  79.     
  80.   if(TLock(conf))
  81.   {
  82.     while(1)
  83.     {
  84.       FileSize=GetFileSize(conf);
  85.       TotalSlots=FileSize/(long)sizeof(struct MailHeader);
  86.       printf("Total Slots = %ld\n",TotalSlots);
  87.       printf("Partial     = %ld\n",FileSize%(long)sizeof(struct MailHeader));
  88.       if(StatNum==-1L)
  89.     { printf("NextMsgNum  = UNKNOWN\n");
  90.       }
  91.  else printf("NextMsgNum  = %ld <---Per MailStats\n",StatNum);
  92.        printf("*---------------------------------------------*\n");
  93.       if(FileSize%(long)sizeof(struct MailHeader))
  94.       {
  95.         Eoj=FALSE;
  96.      printf("\nMsgBase HeaderFile is corrupt                \n");
  97.        printf("Rebuilding HeaderFile                        \n");
  98.         sprintf(TempFile,"%s.old",conf);
  99.         Rename(conf,TempFile);
  100.         fi=fopen(TempFile,"rb");
  101.         fo=fopen(conf,"wb");
  102.         TempSlot=0L;
  103.         while(TempSlot<TotalSlots)
  104.         {
  105.           fread((APTR)&MS,sizeof(struct MailHeader),1,fi);
  106.           fwrite((APTR)&MS,sizeof(struct MailHeader),1,fo);
  107.           TempSlot +=1L;
  108.         }
  109.         fclose(fi);
  110.         fclose(fo);
  111.         DeleteFile(TempFile);
  112.         InitStat();
  113.         OMS.HighMsgNum=MS.MsgNumb+1L;
  114.         fo=fopen(StatFile,"wb");
  115.         fwrite((APTR)&OMS,sizeof(struct MailStat),1,fo);
  116.         fclose(fo);
  117.         printf("MailStats updated\n");
  118.         StatError=FALSE;
  119.       }
  120.       else break;
  121.      }
  122.     fi=fopen(conf,"rb");
  123.     if(fi==NULL)
  124.     {
  125.       printf("Error! can't open file %s\n",conf);Eoj=FALSE;
  126.       end();
  127.     }
  128.     Slot=TotalSlots -1L;
  129.     fseek(fi,(long)sizeof(struct MailHeader)*Slot,SEEK_SET);
  130.     fread((APTR)&MS,sizeof(struct MailHeader),1,fi);
  131.     fclose(fi);
  132.     printf("Current MailHeader for Slot %ld\n",Slot+1);
  133.     printf("\n");
  134.     printf("Status       :%c\n",MS.Status[0]);
  135.     printf("MsgNumb      :%ld\n",MS.MsgNumb);
  136.     printf("ToName       :%s\n",MS.ToName);
  137.     printf("FromName     :%s\n",MS.FromName);
  138.     printf("Subject      :%s\n",MS.Subject);
  139.     printf("MsgDate      :%ld\n",MS.MsgDate);
  140.     printf("Recv         :%ld\n",MS.Recv);
  141.     printf("\n");
  142.     if(StatError)
  143.     {
  144.      InitStat(); Eoj=FALSE;
  145.         OMS.HighMsgNum=MS.MsgNumb+1L;
  146.         fo=fopen(StatFile,"wb");
  147.         fwrite((APTR)&OMS,sizeof(struct MailStat),1,fo);
  148.         fclose(fo);
  149.         printf("MailStats updated\n");
  150.         StatError=FALSE;
  151.     }
  152.     else if(!ValidStat())
  153.     {
  154.         InitStat();Eoj;
  155.       OMS.HighMsgNum=MS.MsgNumb+1L;
  156.         fo=fopen(StatFile,"wb");
  157.         fwrite((APTR)&OMS,sizeof(struct MailStat),1,fo);
  158.         fclose(fo);
  159.         printf("MailStats updated\n");
  160.         StatError=FALSE;
  161.     }
  162.   }
  163.   else
  164.   {
  165.     if(!UPDATE)
  166.     {
  167.       printf("Error!, HeaderFile does not exist for this conference\n");
  168.       printf("This is normal if the message base has not been used\n");
  169.       printf("But if the message base has been used then please run\n");
  170.       printf("this utility with the UPDATE option\n");
  171.       printf("\n\n");
  172.       if(TLock(StatFile))
  173.       {
  174.         DeleteFile(StatFile);
  175.       }
  176.       exit(0);
  177.     }
  178.   }
  179.   if(UPDATE)
  180.   {
  181.     printf("Enter New Status    >:");
  182.     gets(temp);
  183.     MS.Status[0]=temp[0];
  184.     printf("Enter New MsgNum    >:");
  185.     gets(temp);
  186.     MS.MsgNumb=atol(temp);
  187.     printf("Enter New ToName    >:");
  188.     gets(temp);
  189.     strcpy(MS.ToName,temp);
  190.     printf("Enter New FromName  >:");
  191.     gets(temp);
  192.     strcpy(MS.FromName,temp);
  193.     printf("Enter New Subject   >:");
  194.     gets(temp);
  195.     strcpy(MS.Subject,temp);
  196.     printf("Enter New MsgDate   >:");
  197.     gets(temp);
  198.     MS.MsgDate=atol(temp);
  199.     printf("Enter New Recv      >:");
  200.     gets(temp);
  201.     MS.Recv=atol(temp);
  202.  
  203.     fi=fopen(conf,"wb");
  204.     if(fi==NULL)
  205.     {
  206.       printf("Error!, can't create HeaderFile file\n");
  207.       printf("\n\n");Eoj=FALSE;
  208.       end();
  209.     }
  210.     fseek(fi,(long)sizeof(struct MailHeader)*Slot,SEEK_SET);
  211.     
  212.     fwrite((APTR)&MS,sizeof(struct MailHeader),1,fi);
  213.     fclose(fi);
  214.      InitStat();
  215.         OMS.HighMsgNum=MS.MsgNumb+1L;
  216.         fo=fopen(StatFile,"wb");
  217.         fwrite((APTR)&OMS,sizeof(struct MailStat),1,fo);
  218.         fclose(fo);
  219.         printf("MailStats updated\n");
  220.   }
  221.   end();
  222. }
  223. void end()
  224. {
  225.   printf("*---------------------------------------------*\n");
  226.   if(Eoj)
  227.   {
  228.   printf("*     MESSAGE BASE APPEARS TO BE CORRECT      *\n");
  229.   printf("*---------------------------------------------*\n");
  230.   }
  231.   exit(0);
  232. }
  233. void sr(char *str)
  234. {
  235.   register int i;
  236.   i=strlen(str)-1;
  237.   while(i>-1)
  238.   {
  239.     if(*(str+i)<=32) *(str+i)='\0'; else break;
  240.     i--; 
  241.   }
  242. }
  243. #define ACCESS_READ -2
  244. int TLock(char *str)
  245. {
  246.   long lock;
  247.   if(lock=Lock(str,ACCESS_READ))
  248.   {
  249.     UnLock(lock); return(1);
  250.   }
  251.   return(0);
  252. }
  253. void NullStats()
  254. {
  255.   strcpy(MS.Status,"");
  256.   MS.MsgNumb=0L;
  257.   strcpy(MS.ToName,"");
  258.   strcpy(MS.FromName,"");
  259.   strcpy(MS.Subject,"");
  260.   MS.MsgDate=0L;
  261.   MS.Recv=0L;
  262.   MS.Pad=' ';
  263. }
  264. long GetFileSize(char *str)
  265. {
  266. struct FileInfoBlock *FBlock;
  267. long *FLock;
  268.  
  269.   long fsize=-1L;
  270. if((FLock = Lock(str,ACCESS_READ)) == NULL)
  271.         {
  272.           printf("Can't lock file\r\n");
  273.           return(-1L);
  274.         }
  275.         if((FBlock=AllocMem((long)sizeof(struct FileInfoBlock),MEMF_CHIP)) == NULL)
  276.         {
  277.           printf("Out of chipmem for FileInfoBlock\r\n");
  278.           UnLock(FLock);
  279.           return(-1L);
  280.         }
  281.         if( Examine(FLock,FBlock) )
  282.         {
  283.           fsize = FBlock->fib_Size;
  284.         }
  285.         UnLock(FLock);
  286.         FreeMem(FBlock,sizeof(struct FileInfoBlock));
  287.         return(fsize);
  288. }
  289.  
  290. void InitStat()
  291. {
  292.    OMS.LowestKey=1L;
  293.   OMS.HighMsgNum=0L;
  294.   OMS.LowestNotDel=1L;
  295.   strcpy(OMS.Pad,"     ");
  296. }
  297.  
  298. int ValidStat()
  299. {
  300.   FILE *fi;
  301.   if(TLock(StatFile))
  302.   {
  303.     fi=fopen(StatFile,"rb");
  304.     fread((APTR)&OMS,sizeof(struct MailStat),1,fi);
  305.     fclose(fi);
  306.     if(OMS.HighMsgNum!=MS.MsgNumb+1L)
  307.     {
  308.       return(0);
  309.     }
  310.     return(1);
  311.   }
  312.   return(0);
  313. }
  314. long GetStat()
  315. {
  316.   FILE *fi;
  317.    if(TLock(StatFile))
  318.   {
  319.     fi=fopen(StatFile,"rb");
  320.     fread((APTR)&OMS,sizeof(struct MailStat),1,fi);
  321.     fclose(fi);
  322.     return(OMS.HighMsgNum);
  323.   }
  324.   return(-1L);
  325. }